package cn.gok.server.service.impl;

import cn.gok.common.dao.backstage.SysMenuMapper;
import cn.gok.common.entity.backstage.SysMenu;
import cn.gok.server.service.SysMenuService;
import cn.gok.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: DxlinY
 * @apiNote: 菜单服务层实现类
 * @date: 2020/9/26
 * @time: 15:17
 */
@Service(value = "sysMenuServiceImpl")
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List <SysMenu> getMenus() {
        List <SysMenu> menus = sysMenuMapper.getMenusById(UserContext.user().getUserId());
        List <SysMenu> resultMenus = new ArrayList <>();
        if (menus.isEmpty()) {
            return null;
        }
        menus.forEach(menu -> {
            if (menu.getParentId() == -1) {
                List <SysMenu> sonMenus = new ArrayList <>();
                menus.forEach(sonMenu -> {
                    if (sonMenu.getParentId().equals(menu.getMenuId())) {
                        sonMenus.add(sonMenu);
                    }
                });
                menu.setSon(sonMenus);
                resultMenus.add(menu);
            }
        });
        return resultMenus;
    }
}
